//
//  UIController.swift
//  LittleSummary
//
//  Created by 郝旭珊 on 2018/3/24.
//  Copyright © 2018年 郝旭珊. All rights reserved.
//

import UIKit

class UIController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

    }

    func setImageViewAbout(){
        //固定 imageView 的大小
        let thumbnail = UIImageView()
        let itemSize = CGSize(width: 80, height: 80)
        UIGraphicsBeginImageContextWithOptions(itemSize,false,0.0)
        let imageRect = CGRect(x: 0.0, y: 0.0, width: itemSize.width, height: itemSize.height);
        thumbnail.draw(imageRect)
        thumbnail.image
            = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()

        //图片平铺
        let image =  UIImage(named: "")!.resizableImage(withCapInsets: UIEdgeInsets.zero, resizingMode: .tile)
        thumbnail.image = image
    }

    @objc func keyboardWillChangeFrame(_ notification:Notification){
        NotificationCenter.default.addObserver(self, selector: #selector(UIController.keyboardWillChangeFrame(_:)), name: NSNotification.Name.UIKeyboardWillChangeFrame, object: nil)

        // 1.取出键盘动画的时间
        let duration = (notification.userInfo!)[UIKeyboardAnimationDurationUserInfoKey] as! Double

        // 2.取得键盘最后的frame
        let keyboardFrame = ((notification.userInfo!)[UIKeyboardFrameEndUserInfoKey] as! NSValue).cgRectValue

        // 3.计算控制器的view需要平移的距离
        let transformY = keyboardFrame.origin.y - SCREEN_HEIGHT

        // 4.执行动画平移view
        UIView.animate(withDuration: duration, animations: {

            self.view.transform = CGAffineTransform(translationX: 0, y: transformY)
        })
    }


    //键盘左上角添加"完成"按钮
    func textFieldDidBeginEditing(_ textField: UITextField) {
        //添加 toolBar
        let toolBar = UIToolbar(frame: CGRect(x: 0, y: self.view.frame.height - 60, width: SCREEN_WIDTH, height: 60))
        toolBar.backgroundColor = UIColor.white
        let finishBtn = UIButton()
        finishBtn.frame = CGRect(x: 0, y: 0, width: 50, height: 30)
        finishBtn.setTitle("完成", for: UIControlState())
        finishBtn.setTitleColor(UIColor.red, for: UIControlState())
        finishBtn.titleLabel?.font = UIFont.systemFont(ofSize: 13)
        //        finishBtn.addTarget(self, action: #selector(ParameterView.completeItemClick(_:)), forControlEvents: .TouchUpInside)
        let barItem = UIBarButtonItem.init(customView: finishBtn)

        let dict = [NSAttributedStringKey.foregroundColor: UIColor.black]
        barItem.setTitleTextAttributes(dict, for: UIControlState())
        let flexible = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: self, action: nil)
        toolBar.setItems([flexible,barItem], animated: false)
        view.addSubview(toolBar)
    }

}
